from dataclasses import dataclass, field
from typing import Tuple, Dict

@dataclass
class ProcessConfig:
    """图片处理配置参数"""
    
    # 基础质量参数
    quality_threshold: float = field(
        default=0.5,
        metadata={
            'description': '图片质量阈值',
            'range': (0, 1.0),
            'recommended': 0.5,
            'help': '值越大表示要求越高，建议范围0.3-0.7'
        }
    )
    
    min_resolution: Tuple[int, int] = field(
        default=(512, 512),
        metadata={
            'description': '最小分辨率要求(宽x高)',
            'range': ((384, 384), (None, None)),
            'recommended': (512, 512),
            'help': '建议使用不小于512x512的图片'
        }
    )
    
    max_resolution: Tuple[int, int] = field(
        default=(4096, 4096),
        metadata={
            'description': '最大分辨率限制(宽x高)',
            'range': ((1024, 1024), (8192, 8192)),
            'recommended': (4096, 4096),
            'help': '过大的分辨率会增加训练时间和显存占用，建议限制在4096x4096以内'
        }
    )
    
    # 人脸检测参数
    face_ratio_min: float = field(
        default=0.15,
        metadata={
            'description': '最小人脸占比',
            'range': (0.05, 0.3),
            'recommended': 0.15,
            'help': '人脸区域占整张图片的最小比例，过小的人脸不利于特征学习'
        }
    )
    
    face_ratio_max: float = field(
        default=0.75,
        metadata={
            'description': '最大人脸占比',
            'range': (0.4, 0.9),
            'recommended': 0.75,
            'help': '人脸区域占整张图片的最大比例，过大表示人脸太近'
        }
    )
    
    yaw_range: Tuple[float, float] = field(
        default=(-30, 30),
        metadata={
            'description': '偏航角范围(度)',
            'range': ((-45, 45), (-15, 15)),
            'recommended': (-30, 30),
            'help': '脸部左右转动的角度范围，过大的角度会导致面部特征不清晰'
        }
    )
    
    pitch_range: Tuple[float, float] = field(
        default=(-20, 20),
        metadata={
            'description': '俯仰角范围(度)',
            'range': ((-30, 30), (-10, 10)),
            'recommended': (-20, 20),
            'help': '脸部上下转动的角度范围，过大的角度会改变面部比例'
        }
    )
    
    blur_threshold: float = field(
        default=50,
        metadata={
            'description': '模糊度阈值',
            'range': (20, 100),
            'recommended': 50,
            'help': '基于Laplacian算子的清晰度评估，值越小表示越模糊'
        }
    )
    
    # 光照参数
    exposure_range: Tuple[float, float] = field(
        default=(0.05, 0.95),
        metadata={
            'description': '曝光度范围',
            'range': ((0, 0.1), (0.9, 1.0)),
            'recommended': (0.05, 0.95),
            'help': '控制图片的明暗分布，避免过暗或过亮'
        }
    )
    
    # 聚类参数
    cluster_threshold: float = field(
        default=0.3,
        metadata={
            'description': '聚类相似度阈值',
            'range': (0.1, 0.5),
            'recommended': 0.2,
            'help': '控制图片去重的严格程度，值越小要求越严格'
        }
    )
    
    min_cluster_size: int = field(
        default=3,
        metadata={
            'description': '最小聚类大小',
            'range': (1, 10),
            'recommended': 3,
            'help': '每个聚类最少包含的图片数量，用于控制样本多样性'
        }
    )
    
    # 图片增强参数
    enhance_contrast: bool = field(
        default=True,
        metadata={
            'description': '增强对比度',
            'help': '增强图片的对比度'
        }
    )
    
    enhance_sharpness: bool = field(
        default=True,
        metadata={
            'description': '增强锐度',
            'help': '增强图片的锐度'
        }
    )
    
    normalize_lighting: bool = field(
        default=True,
        metadata={
            'description': '标准化光照',
            'help': '标准化图片的光照'
        }
    )
    
    def __post_init__(self):
        """初始化后的处理"""
        # 确保所有字段都有默认值
        if not hasattr(self, 'quality_threshold'):
            self.quality_threshold = 0.5
        if not hasattr(self, 'min_resolution'):
            self.min_resolution = (512, 512)
        if not hasattr(self, 'enhance_contrast'):
            self.enhance_contrast = True
        if not hasattr(self, 'enhance_sharpness'):
            self.enhance_sharpness = True
        if not hasattr(self, 'normalize_lighting'):
            self.normalize_lighting = True
    
    def to_dict(self) -> Dict:
        """将配置转换为字典格式"""
        return {
            'quality_threshold': self.quality_threshold,
            'min_resolution': self.min_resolution,
            'max_resolution': self.max_resolution,
            'face_ratio_range': (self.face_ratio_min, self.face_ratio_max),
            'yaw_range': self.yaw_range,
            'pitch_range': self.pitch_range,
            'blur_threshold': self.blur_threshold,
            'exposure_range': self.exposure_range,
            'cluster_threshold': self.cluster_threshold,
            'min_cluster_size': self.min_cluster_size,
            'enhance_contrast': self.enhance_contrast,
            'enhance_sharpness': self.enhance_sharpness,
            'normalize_lighting': self.normalize_lighting
        }
    
    @classmethod
    def get_param_info(cls) -> Dict:
        """获取所有参数的说明信息"""
        return {
            field.name: field.metadata
            for field in cls.__dataclass_fields__.values()
        } 